<?php
  if(!$_SESSION['MczSession']->can('add_file'))
  {
    die();
  }  

  //register_shutdown_function('cleanShutdown');

  function printFilesInfos($files)
  {
    
  }
  
  function cleanShutdown()
  {
    header("Location: index.php?c=import");
  }
 
/******************************************************************************/ 
  
  $ready_files  = array();
  $errors       = array();
  
  if(isset($_POST['token']))
  {
    if($_POST['token'] == $_SESSION['MczSession']->getToken())
    {
      if(!empty($_FILES))
      {
        foreach($_FILES as $file)
        {
          if($file['error'] == '0')
          {
            move_uploaded_file($file['tmp_name'],$_SESSION['MczSession']->getHomeDir().'/local_import/'.$file['name']);
            chmod($_SESSION['MczSession']->getHomeDir().'/local_import/'.$file['name'],0777);
          }
        }
      }
      elseif(isset($_POST['addUrl']))
      {
        ImportCue::add($_POST['addUrl']);
      }
      
      if(isset($_POST['importEmAll']))
      {
        $maxtime = ini_get("max_execution_time") - 3;
        $startime = time();
        $spentime = 0;
        $ready_files = ImportCue::getReadyFiles();
        foreach($ready_files as $file)
        {
          if($spentime>$maxtime)
          {
            break;
          }
          chmod($file,0777);
          if(ImportCue::import($file) != '1')
          {
            $errors[] = $file;
          }
          $spentime = time() - $startime;
        }
      }
      if(isset($_POST['delEmAll']))
      {
        $ready_files = ImportCue::getReadyFiles();
        foreach($ready_files as $file)
        {
          chmod($file,0777);
          unlink($file);
        }
      }
    }
  }
  
  $ready_files    = ImportCue::getReadyFiles();
  $unready_files  = ImportCue::getUnreadyFiles();
  include(APP_PATH.'app/mvc/v/import_v.php');
  die();
?>
